package com.jlnk.rxjavademo2.view;

import android.os.Bundle;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.bumptech.glide.Glide;
import com.jlnk.rxjavademo2.R;
import com.jlnk.rxjavademo2.entity.WeatherEntity;
import com.jlnk.rxjavademo2.presenter.WeatherPresenter;

public class WeatherActivity extends AppCompatActivity implements WeatherView {
    private TextView weatherTextView;
    private TextView temperatureTextView;
    private ImageView weatherIcon;
    private ProgressBar progressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_weather);
        progressBar = findViewById(R.id.progressBar);
        weatherTextView = findViewById(R.id.weatherTextView);
        temperatureTextView = findViewById(R.id.temperatureTextView);
        weatherIcon = findViewById(R.id.weatherIcon);
        init();
    }

    private void init() {
        WeatherPresenter presenter = new WeatherPresenter(this, "aea1852529f64cc6bdc6a81da2e0b9db");
        presenter.loadWeatherInfo("Chinese", null, "BeiJing");
    }

    @Override
    public void showLoading() {
        progressBar.setVisibility(ProgressBar.VISIBLE);
    }

    @Override
    public void hideLoading() {
        progressBar.setVisibility(ProgressBar.GONE);
    }

    @Override
    public void showWeatherInfo(WeatherEntity.WeatherData weatherData) {
        String iconCode = weatherData.getWeather().getIcon();
        String iconUrl = "https://cdn.weatherbit.io/static/img/icons/" + iconCode + ".png";
        weatherTextView.setText(weatherData.getWeather().getDescription());
        temperatureTextView.setText("Temperature: " + weatherData.getWeather().getCode());
        Glide.with(this)
                .load(iconUrl)
                .placeholder(R.drawable.ic_launcher_background)
                .error(R.drawable.ic_launcher_foreground)
                .into(weatherIcon);
    }

    @Override
    public void showError(String errorMessage) {
        hideLoading();
    }
}